﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using WebClient;
namespace WebClient.admin.Modules.products
{
    public partial class Edit : System.Web.UI.UserControl
    {
        private static String patthOld = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
            if (!this.IsPostBack)
            {
                LoadGroupProduct();
                InitItem();
            }
        }
        private void LoadGroupProduct()
        {
            WebData db = new WebData();
            this.cbbgroupId.DataSource = db.GetAllGroupproducts();
            this.cbbgroupId.DataValueField = "id";
            this.cbbgroupId.DataTextField = "name";
            this.cbbgroupId.DataBind();

            this.cbbVendorId.DataSource = db.GetAllVendor();
            this.cbbVendorId.DataValueField = "id";
            this.cbbVendorId.DataTextField = "name";
            this.cbbVendorId.DataBind();
        }
        private void AddNew()
        {
            long groupId = 1;
            
            FileUploads file = new FileUploads();

            String sPath = "";
            sPath = file.FileSaveImage("/images/sanpham", this.pathImage);
            if (!String.IsNullOrEmpty(sPath))
            {
                file.DeleteFile(patthOld);
            }
            else {
                sPath = patthOld;
            }

            int vendorId = 1;
            try
            {
                groupId = Convert.ToInt64(this.cbbgroupId.SelectedValue);
                vendorId = Convert.ToInt32(this.cbbVendorId.SelectedValue);
            }
            catch { }

            WebData data = new WebData();
            DTO.Products item = new DTO.Products();
            item.id = Convert.ToInt64(this.txtid.Text.Trim());
            item.groupProductId = groupId;// Convert.ToInt64(this);
            item.productName = this.txtproductName.Text.Trim();
            item.description = this.txtdescription.Text.Trim();
            item.create_on = DateTime.Now;
            item.price = Convert.ToDecimal(this.txtprice.Text.Trim());
            item.updatePriceLast = item.price;
            item.pathImage = sPath;
            item.content = this.txtcontent.Text;
            item.isActive = 1;
            item.vendorId = vendorId;
            data.OnSubmitProducts(item, 1);
            
        }
        private void InitItem()
        {
            ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
            WebData db = new WebData();
            DTO.Products item = new DTO.Products();
            long id = -1;
            try {
                id = Convert.ToInt64(Request.Params["ID1"]);
            }
            catch { }
            item = db.GetItemByIdProducts(id);
            if (item != null)
            {
                txtid.Text = item.id.ToString();
                cbbgroupId.SelectedItem.Value = item.groupProductId.ToString();
                txtproductName.Text = item.productName.ToString();
                txtdescription.Text = item.description.ToString();                
                txtprice.Text = item.price.ToString();
                patthOld = item.pathImage;
                txtcontent.Text = item.content;
                
            }

        }
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            AddNew();
        }
    }
}